package com.hzh.homeWork

import scala.io.Source


/**
 * 2、统计学生的总分
 */
object DemoWork2 {
  def main(args: Array[String]): Unit = {
    val lines: List[String] = Source.fromFile("data/score.txt").getLines().toList
    //将ID和成绩切分出来
    val id: List[(String, Int)] = lines.map((lines: String) => {
      val split: Array[String] = lines.split(",")
      (split(0),
        (if ((split.last.toInt) <= 150) {
          split.last
        } else {
          "0"
        }).toInt
      )
    })

    //按照id进行分组
    val groupBy: Map[String, List[(String, Int)]] = id.groupBy(kv => kv._1)

    val sum_score: Map[String, Int] = groupBy.map((kv: (String, List[(String, Int)])) => {
      val id: String = kv._1
      val score: List[(String, Int)] = kv._2
      val sum: List[Int] = score.map(kv => kv._2)
      (id, sum.sum)
    })
    sum_score.foreach(println)
  }
}
